/// <reference path="../libs/Cesium/Cesium.js" />

let MousePositionControl = (function () {

    /**
     * 显示经纬度控件
     * @param {*} element 容器
     * @param {*} viewer Viewer
     */
    function MousePositionControl(element, viewer) {
        this.container = $(element);
        this.viewer = viewer;
        this.container.attr("class", "mouse-position");

        let handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);

        handler.setInputAction(e => {
            this.showMousePosition(e);
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    }

    /**
     * 显示经纬度
     */
    MousePositionControl.prototype.showMousePosition = function (e) {
        let pick = new Cesium.Cartesian2(e.endPosition.x, e.endPosition.y);
        let cartesian = this.viewer.scene.globe.pick(this.viewer.camera.getPickRay(pick), this.viewer.scene);
        if (cartesian) {
            let cartographic = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
            let lng = Cesium.Math.toDegrees(cartographic.longitude);
            let lat = Cesium.Math.toDegrees(cartographic.latitude);
            let height = cartographic.height;

            this.container.text(lng.toFixed(7) + ", " + lat.toFixed(7) + ", " + height.toFixed(1)); // 显示经纬度
        }
    }

    return MousePositionControl;
})();
